<?php

global $debug;

$debug = array(
    'templates' => array(),
    'queries' => array(),
    'variables' => array()
);

function dvar($var_val, $var_name, $i = 0) {
	$mod_var_name = $var_name . ($i ? '_#'.$i : '');

	if (!isset($GLOBALS['debug']['variables'][(string)$mod_var_name])) {
		$GLOBALS['debug']['variables'][(string)$mod_var_name] = $var_val;
		return;
	} else {
		$i = $i+1;
		return dvar($var_val, $var_name, $i);
	}
}
function dquery($query) {
    $GLOBALS['debug']['queries'][] = $query;
}
function dtpl($tpl_name) {
    $GLOBALS['debug']['templates'][] = $tpl_name;
}

function HTML_ESC($_input = null, $_esc_keys = false)
{
    if ((null != $_input) && (is_array($_input)))
    {
        foreach($_input as $key => $value)
        {
            if($_esc_keys)
            {
                $_return[htmlspecialchars($key)] = HTML_ESC($value,$_esc_keys);
            }
            else
            {
                $_return[$key] = HTML_ESC($value);
            }
        }
        return $_return;
    }
	elseif(is_object($_input)) {
		return $_input;
	}
    elseif(null != $_input)
    {
        return htmlspecialchars($_input);
    }
    else
    {
        return null;
    }
}

function display_console() {
    ob_start();
?>
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title>Debug Console</title>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type="text/css">
body, h1, h2, td, th, p {
    font-family: sans-serif;
    font-weight: normal;
    font-size: 0.9em;
    margin: 1px;
    padding: 0;
}

h1 {
    margin: 0;
    text-align: left;
    padding: 2px;
    background-color: #f0c040;
    color:  black;
    font-weight: bold;
    font-size: 1.2em;
 }

h2 {
    background-color: #9B410E;
    color: white;
    text-align: left;
    font-weight: bold;
    padding: 2px;
    border-top: 1px solid black;
}

p, table, div {
    background: #f0ead8;
} 

p {
    margin: 0;
    font-style: italic;
    text-align: center;
}

table {
    width: 100%;
}

th, td {
    font-family: monospace;
    vertical-align: top;
    text-align: left;
    width: 50%;
}

td {
    color: green;
}

.odd {
    background-color: #eeeeee;
}

.even {
    background-color: #fafafa;
}

.exectime {
    font-size: 0.8em;
    font-style: italic;
}

#table_assigned_vars th {
    color: blue;
}

#table_config_vars th {
    color: maroon;
}
</style>
</head>
<body>

<h1>Debug Console</h1>
<h2>Included templates</h2>

<table id="table_assigned_vars">
    <?php 
        $i = 0;
        foreach ($GLOBALS['debug']['templates'] as $vname => $vval) {
            $class = (($i % 2) ? 'odd' : 'even');
            $i++;
    ?>
        <tr class="<?php echo $class; ?>">
            <td><pre><?php print_r($vval); ?></pre></td></tr>
    <?php } ?>
</table>

<h2>Queries</h2>

<table id="table_assigned_vars">
    <?php 
        $i = 0;
        foreach ($GLOBALS['debug']['queries'] as $vname => $vval) {
            $class = (($i % 2) ? 'odd' : 'even');
            $i++;
    ?>
        <tr class="<?php echo $class; ?>">
            <th><?php echo $vname; ?></th>
            <td><pre><?php print_r($vval); ?></pre></td></tr>
    <?php } ?>
</table>


<h2>Variables</h2>

<table id="table_assigned_vars">
    <?php 
        $i = 0;
        foreach ($GLOBALS['debug']['variables'] as $vname => $vval) {
            $class = (($i % 2) ? 'odd' : 'even');
            $i++;
    ?>
        <tr class="<?php echo $class; ?>">
            <th><?php echo $vname; ?></th>
            <td><pre><?php print_r(HTML_ESC($vval)); ?></pre></td></tr>
    <?php } ?>
</table>

</body>
</html>

<?php
$console_str = ob_get_contents();
ob_end_clean();
$console_str = strtr($console_str, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));

?>
<script type="text/javascript">
    if ( self.name == '' ) {
       var title = 'Console';
    }
    else {
       var title = 'Console_' + self.name;
    }
    console = window.open("",title.value,"width=680,height=600,resizable,scrollbars=yes");
    console.document.write('<?php echo $console_str; ?>');
    console.document.close();
</script>

<?php

}

?>
